package com.meida.msg.provider.listener;

import com.alibaba.fastjson.util.TypeUtils;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.constants.QueueConstants;
import com.meida.msg.provider.websocket.WebSocketServerEndpoint;
import com.meida.starter.rabbitmq.config.RabbitComponent;
import com.meida.starter.rabbitmq.core.BaseRabbiMqHandler;
import com.meida.starter.rabbitmq.listener.MqListener;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;
import java.util.List;

/**
 * 搜索关机键记录
 *
 * @author zyf
 */
@RabbitComponent(value = "webSocketRabbitMqListener")
public class WebSocketRabbitMqListener extends BaseRabbiMqHandler<EntityMap> {

    @Resource
    private WebSocketServerEndpoint webSocketServerEndpoint;

    @RabbitListener(queues = QueueConstants.QUEUE_WEBSOCKET_MSG)
    public void onMessage(EntityMap baseMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        super.onMessage(baseMap, deliveryTag, channel, new MqListener<EntityMap>() {
            @Override
            public void handler(EntityMap map, Channel channel) {
                List<Long> receiveUserIds = (List<Long>) map.get("receiveUserIds");
                Integer sendType = TypeUtils.castToInt(map.get("sendType"));
                if (FlymeUtils.isNotEmpty(receiveUserIds)) {
                     webSocketServerEndpoint.pushMessage(receiveUserIds, sendType, map);
                }
            }
        });
    }
}
